package com.boot2551.control;

import com.boot2551.bean.Dept;
import com.boot2551.bean.Dept2;
import com.boot2551.service.DeptS;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "dept")
public class DeptC {
    //按照名称自动装配
    @Resource
    public DeptS ds;

    /**
     *
     * @param d 参数通过框架自动注入，页面参数与属性一致
     * @return  @ResponseBody将方法的返回数据转json串写入页面中
     */
    @RequestMapping(value = "/insert")
    @ResponseBody
    public int insert(@RequestBody Dept d){
        return ds.insert(d);
    }

    /**
     * 只能发送get请求
     * @return  将集合数据转json串写入页面
     */
    @GetMapping(value = "/all")
    @ResponseBody
    public List<Dept> all(){
        return ds.all();
    }/**
     * 只能发送get请求
     * @return  将集合数据转json串写入页面
     */
    @GetMapping(value = "/all2")
    @ResponseBody
    public List<Dept2> all2(){
        return ds.all2();
    }

    /**
     * RequestMapping支持get和post请求
     * @param id
     * @return
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public int del(int id){
        return ds.delete(id);
    }
    @DeleteMapping(value = "/del2")
    @ResponseBody
    public int del2(int id){
        return ds.delete(id);
    }
    @GetMapping(value = "/one")
    @ResponseBody
    public Dept one(int id){
        return ds.one(id);
    }
    @RequestMapping(value = "/update")
    @ResponseBody
    public int update(Dept d){

        return  ds.update(d);
    }
    @PutMapping(value = "/update2")
    @ResponseBody
    public int update2(@RequestBody Dept d){
        return  ds.update(d);
    }
    @GetMapping(value = "/page")
    @ResponseBody
    public List<Dept> page(int cur){
        return ds.page((cur-1)*5,5);
    }
    @GetMapping(value = "/find")
    @ResponseBody
    public List<Dept> find(Dept d){
        Map<String,Object> data=new HashMap<>();
        data.put("dname",d.getDname());
        data.put("loc",d.getLoc());
        return ds.find(data);
    }
    @GetMapping(value = "/insert2")
    @ResponseBody
    public int insert2(Dept d){
        return  ds.insertSelective(d);
    }
    @GetMapping(value = "/update3")
    @ResponseBody
    public int update3(Dept d){
        return  ds.updateSelective(d);
    }

    /**
     * @ResponseBody 是一个类注解，也是一个方法注解
     * @RequestBody是一个参数注解，将json转对象
     * @param d
     * @return
     */
    @GetMapping(value = "/insert3")
    @ResponseBody
    public int insert3(@RequestBody Dept d){
        return ds.insertSelective(d);
    }
    @GetMapping(value = "/insert4")
    @ResponseBody
    public int insertBatch(@RequestBody List<Dept> d){
        return ds.insertBatch(d);
    }
    @GetMapping(value = "/insert5")
    @ResponseBody
    public int insertBatch2(@RequestBody List<Dept> d){
        Map<String,Object> data=new HashMap<>();
        data.put("map",d);
        return ds.insertBatch2(data);
    }

}
